﻿using Core;
using ET;
using UnityEngine;

public class EnemyHelper
{
    public static async ETTask<Unit> Create(MapLevelConfig config)
    {
        var unitConfig = World.GetComponent<ConfigComponent>().GetConfig<UnitConfig>(config.unitId);
        if (unitConfig == null)
            return default;

        var player = Player.Instance.GetParent<Unit>();
        var unitComponent = World.GetComponent<UnitComponent>();
        var unit = await unitComponent.CreateAsync(player.Scene, unitConfig.id);
        unit.model.gameObject.layer = LayerMask.NameToLayer("Enemy");
        unit.AddComponent<Enemy>();
        
        var stateMachine = unit.GetComponent<StateMachineComponent>();
        stateMachine.AddState(StateType.Idle, new IdleState());
        stateMachine.AddState(StateType.Move, new EnemyMoveState(unit));
        stateMachine.ChangedState(StateType.Idle);
        return unit;
    }
}